home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15547 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  88 lines

  1. Path: news.interlog.com!news
  2. From: willer@interlog.com (Steve Willer)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: auto_ptr capability question
  5. Date: Fri, 05 Apr 1996 20:26:52 GMT
  6. Organization: InterLog Internet Services
  7. Message-ID: <316577c3.392060888@news.interlog.com>
  8. References: <31600aa3.1288774@news.xmission.com> <31630583.85660528@news.xmission.com>
  9. NNTP-Posting-Host: ip93-211.tor.interlog.com
  10. X-Newsreader: Forte Agent .99d/32.182
  11.  
  12. macron@xmission.com (Joe Schlimgen) wrote:
  13.  
  14. >True, but a minor detail that has nothing to do with the problem...
  15.  
  16. I know. I just wanted to be sure you were clear on it.
  17.  
  18. >I agree that a few extra keystrokes that avoid possible problems are
  19. >well worth it. I've also been using my auto_ptr-like class and a string
  20. >class that have implicit conversions with no problems (unless they're
  21. >**very** subtle). Mostly, the implicit conversions have been used for
  22. >parameters to older functions. Can you give any examples of the type of
  23. >problems you're talking about?
  24.  
  25. From what I remember, the problem isn't that it will introduce many subtle bugs
  26. into your code. In fact, the problem is that you might get conversions at times
  27. when you don't want one. I wish I could give you an example, but I don't
  28. remember any and I don't have any of my programming books at home (it's a
  29. holiday here in Canada today). If no one's posted one by Monday, I'll send you
  30. one.
  31.  
  32. Alternatively, you could go out to a bookstore and get "Effective C++" and
  33. "More Effective C++". I'm telling you, it's helped me to clean up potential
  34. errors in my coding.
  35.  
  36. >I've always understood the transfer of ownership. Let me change your
  37. >example just a bit to show the problem I'm talking about:
  38. >
  39. >auto_ptr<T> a(new T);        // Given 'a' something to manage
  40. >auto_ptr<T> b(new T);
  41. >a = b;
  42.  
  43. I took another look at your auto_ptr class. It looks like your reset() function
  44. isn't right. It should be deleting the pointer, then setting it to the
  45. argument. Here's the auto_ptr I use, which is included in Meyers's book but
  46. adapted so it will compile in Borland C++ 4.5 (which doesn't have member
  47. templates or the explicit keyword):
  48.  
  49. // This "auto_ptr" is a smart pointer class that is part of the latest WP
  50. // and should be in BC5.0. This particular implementation was taken from
  51. // "More Effective C++" by Meyers, page 293
  52. template <class T> class auto_ptr {
  53.    public:
  54.       /* explicit */ auto_ptr(T *p=0): pointee(p) {}
  55. //      template<class U> auto_ptr(auto_ptr<U> &rhs): pointee(rhs.release()) {}
  56.       auto_ptr(auto_ptr<T> &rhs): pointee(rhs.release()) {}
  57.       ~auto_ptr() {delete pointee;}
  58. //      template<class U> auto_ptr<T>& operator=(auto_ptr<U> &rhs) {
  59.       auto_ptr<T> &operator=(auto_ptr<T> &rhs) {
  60.          if (this != &rhs) reset(rhs.release());
  61.          return *this;
  62.       }
  63.       T& operator*() const {return *pointee;}
  64.       T* operator->() const {return pointee;}
  65.       T* get() const {return pointee;}
  66.       T* release() {
  67.          T *oldPointee = pointee;
  68.          pointee = 0;
  69.          return oldPointee;
  70.       }
  71.       void reset(T *p=0) {delete pointee; pointee = p;}
  72.    private:
  73.       T *pointee;
  74. };
  75.  
  76. >auto_ptr<T> a(new T);
  77. >auto_ptr<T> b(new T);
  78. >a = b;
  79.  
  80. Now, on the "a=b" line, the second T would get deleted, "a" would point to
  81. nothing, and "b" would point to the first T.
  82.  
  83. >(Talk about subtle bugs...) This places the responsibility of deleting
  84. >the previous object on the programmer. And you know how responsible we
  85. >are. :)
  86.  
  87. Oh yes. Yes, I do.
  88.